﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ViewProduct : System.Web.UI.Page
{
    Product pd = new Product();
    public string Catalog(object obj)
    {
        return pd.GetCatalogName(obj.ToString());
    }
    public string Categories(object obj)
    {
        return pd.GetCategoryName(obj.ToString());
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack==false)
        {
            grvData.DataSource = pd.GetProduct();
            grvData.DataBind();
        }
    }
    protected void btnSearch0_Click(object sender, EventArgs e)
    {
        if(""!= txtProduct.Text.Trim())
        {
            grvData.DataSource = pd.GetProductByName(txtProduct.Text);
            grvData.DataBind();
        }
        else
        {
            lblError1.Visible =true;
            lblError1.Text ="Enter Produt name !";
        }
    }
    protected void grvDataDelete(object sender, GridViewDeleteEventArgs e)
    {
        string id = grvData.DataKeys[e.RowIndex].Value.ToString();
        pd.DelProduct(id);
        grvData.DataSource = pd.GetProduct();
        grvData.DataBind();
    }
    protected void grvDataEdit(object sender, GridViewEditEventArgs e)
    {
        grvData.EditIndex = e.NewEditIndex;
        grvData.DataSource = pd.GetProduct();
        grvData.DataBind();
    }
    protected void grvDataUpload(object sender, GridViewUpdateEventArgs e)
    {
        float price =float.Parse((grvData.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text);
        if (pd.UpdateProduct2(grvData.DataKeys[e.RowIndex].Value.ToString(), (grvData.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text, price))
        {
            grvData.DataSource = pd.GetProduct();
            grvData.DataBind();
        }
    }
    protected void grvDataCancel(object sender, GridViewCancelEditEventArgs e)
    {
        grvData.EditIndex = -1;
        grvData.DataSource = pd.GetProduct();
        grvData.DataBind();
    }
    protected void grvDataEditAll(object sender, System.Web.UI.WebControls.CommandEventArgs e)
    {
        string opt = e.CommandName;
        string id = (string)e.CommandArgument;
        int i = int.Parse(id);
        Label la = (Label)(grvData.Rows[i].FindControl("lblProductID"));
        string prid = la.Text.Trim();
        if (opt.Equals("Edit ALL"))
        {
            Session["PRID"] = prid;
            Response.Redirect("EditProduct.aspx");

        }
    }
    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddProduct.aspx");
    }
    protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvData.PageIndex = e.NewPageIndex;
        grvData.DataSource = pd.GetProduct();
        grvData.DataBind();
    }
}